home *** CD-ROM | disk | FTP | other *** search
/ Aminet 34 / Aminet 34 (2000)(Schatztruhe)[!][Dec 1999].iso / Aminet / util / gnu / unixcmds.lha / unixcmds / src / width.c < prev   
Encoding:
C/C++ Source or Header  |  1999-10-06  |  3.6 KB  |  129 lines

  1. /* width - force a file to a fixed width        Author: Andy Tanenbaum */
  2.  
  3. /* This program reads an input file (by default, stdin), and forces each
  4.  * line to a certain length (by default 80 columns).  Lines that are
  5.  * shorter than this are padded with spaces.  Lines that are longer are
  6.  * truncated.  Tabs are expanded to spaces in the output.
  7.  *
  8.  * Examples:    width x y       # copy x to y and make all lines 80 cols
  9.  *              width x         # copy x to stdout and force all lines to 80
  10.  *              width -40 x     # copy x to stdout and force to 40 cols
  11.  *              width -30 x y   # copy x to y and force to 30 cols
  12.  *              width -20       # copy stdin to stdout and force to 20 cols
  13.  */
  14.  
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17.  
  18. #define MAX_WIDTH       1024    /* longest acceptable line */
  19. #define TAB_WIDTH          8    /* spacing between tab stops */
  20.  
  21. int main  (int argc, char **argv);
  22. void process  (FILE *in, FILE *out, int width);
  23. void cant_open  (char *program, char *file);
  24. void usage  (char *s);
  25.  
  26. int main(argc, argv)
  27. /* [<][>][^][v][top][bottom][index][help] */
  28. int argc;
  29. char *argv[];
  30. {
  31.  
  32.   int width, k;
  33.   char *p;
  34.   FILE *in, *out;
  35.  
  36.   /* Process command line. */
  37.   p = argv[1];
  38.   k = 1;
  39.   width = 80;
  40.   if (argc > 1 && *p == '-') {
  41.         width = atoi(p + 1);
  42.         if (width < 1 || width > MAX_WIDTH) usage(argv[0]);
  43.         k = 2;
  44.   }
  45.   if (argc == k) {
  46.         /* No file arguments. */
  47.         process(stdin, stdout, width);
  48.   } else if (argc == k + 1) {
  49.         /* For example: width file   or   width -30 file */
  50.         if ((in = fopen(argv[k], "r")) == NULL)
  51.                 cant_open(argv[0], argv[k]);
  52.         process(in, stdout, width);
  53.   } else if (argc == k + 2) {
  54.         /* For example, width inf outf  or   width -30 inf outf */
  55.         if ((in = fopen(argv[k], "r")) == NULL)
  56.                 cant_open(argv[0], argv[k]);
  57.         if ((out = fopen(argv[k + 1], "w")) == NULL)
  58.                 cant_open(argv[0], argv[k]);
  59.         process(in, out, width);
  60.   } else {
  61.         usage(argv[0]);
  62.   }
  63.   return(0);
  64. }
  65.  
  66.  
  67. void process(in, out, width)
  68. /* [<][>][^][v][top][bottom][index][help] */
  69. FILE *in, *out;
  70. int width;
  71. {
  72. /* Copy in to out and force all the lines to width. */
  73.  
  74.   int col, spaces;
  75.   register char *p, *q;
  76.   char in_buf[MAX_WIDTH + 1], out_buf[MAX_WIDTH + 1];
  77.  
  78.   while (fgets(in_buf, MAX_WIDTH, in) != NULL) {
  79.         /* This loop executed once for each line in the input file. */
  80.         p = in_buf;
  81.         q = out_buf;
  82.         col = 0;
  83.         while (*p != 0) {
  84.                 /* This loop executed once for each character in the line. */
  85.                 if (*p != '\t') {
  86.                         /* Not a tab. */
  87.                         *q++ = *p++;
  88.                         col++;
  89.                 } else {
  90.                         /* Tab. */
  91.                         spaces = TAB_WIDTH - (col % TAB_WIDTH);
  92.                         col += spaces;
  93.                         while (spaces--) *q++ = ' ';
  94.                         p++;
  95.                 }
  96.         }
  97.         if (*(q - 1) == '\n') *(q - 1) = ' ';
  98.         while (q < &out_buf[width]) *q++ = ' ';
  99.         out_buf[width] = '\n';
  100.         out_buf[width + 1] = 0;
  101.         fputs(out_buf, out);
  102.   }
  103. }
  104.  
  105.  
  106. void cant_open(program, file)
  107. /* [<][>][^][v][top][bottom][index][help] */
  108. char *program;
  109. char *file;
  110. {
  111.   fputs(program, stderr);
  112.   fputs(": cannot open ", stderr);
  113.   fputs(file, stderr);
  114.   putc('\n', stderr);
  115.   exit(1);
  116. }
  117.  
  118.  
  119. void usage(s)
  120. /* [<][>][^][v][top][bottom][index][help] */
  121. char *s;
  122. {
  123.   fputs("Usage: ", stderr);
  124.   fputs(s, stderr);
  125.   fputs(" [-<width>] [infile [outfile] ]\n", stderr);
  126.   exit(1);
  127. }
  128. /* [<][>][^][v][top][bottom][index][help] */
  129.